qx.Class.define("caraffa.forms.StockManejo",
{
extend : qx.ui.form.ow.FormTab,
construct : function ()
{
	//RPC
	this._rpcSManejo = new qx.io.remote.Rpc();
	this._rpcSManejo.setTimeout(10000);
	this._rpcSManejo.setUrl("services/");
	this._rpcSManejo.setServiceName("stockManejo");
	
	this.debug("1");
	
	this._grpStockManejo = new qx.ui.groupbox.GroupBox();
		this._grpStockManejo.setLayout(new qx.ui.layout.Basic());		
		this._txtFiltro = new qx.ui.form.ow.TextField("Filtro:");
		this._cmbRubro = new qx.ui.form.ow.ComboBox("","",true);
			this._cmbRubro.getLabel().setWidth(0);
		this._btnRefrescar = new qx.ui.form.Button("Refrescar", "caraffa/16/actualizar.png");
		this._mdlStockManejo = new qx.ui.table.model.Simple();
			this._mdlStockManejo.setColumns(["Código","Rubro","Artículo", "Marca", "Vencimiento", "Cant."]);
		this._tblStockManejo = new qx.ui.table.Table(this._mdlStockManejo);
			this._tblStockManejo.setStatusBarVisible(true);
			this._tblStockManejo.setColumnWidth(0, 70);
			this._tblStockManejo.setColumnWidth(1, 120);
			this._tblStockManejo.setColumnWidth(2, 400);
			this._tblStockManejo.setColumnWidth(3, 160);
			this._tblStockManejo.setColumnWidth(4, 90);
			this._tblStockManejo.setColumnWidth(5, 70);
			this._tblStockManejo.setShowCellFocusIndicator(false);
			this._tblStockManejo.setWidth(960);
			this._tblStockManejo.setHeight(250);

	this.debug("2");
			
	this._grpStockManejo.add(this._txtFiltro,{top:0, left:0});			
	this._grpStockManejo.add(this._cmbRubro,{top:0, left:330});
	this._grpStockManejo.add(this._btnRefrescar,{top:0, left:550});
	this._grpStockManejo.add(this._tblStockManejo,{top:50, left:0});

	this.debug("3");
	
	this._grpStockManejo1 = new qx.ui.groupbox.GroupBox();
		this._grpStockManejo1.setLayout(new qx.ui.layout.Basic());
		this._grpStockManejo1.setWidth(700);
		this.debug("3-1");
		this._txtCantidad = new qx.ui.form.ow.TextField("Cant. Extrae:");
		this._txdFechaExtrae = new qx.ui.form.ow.DateText("Fecha:");
		this.debug("3-2");
		this._txaMotivo = new qx.ui.form.ow.TextArea("Motivo:","","",true);
			this._txaMotivo.getText().setWidth(550);
			this._txaMotivo.getText().setHeight(50);

		this.debug("3-3");
		this._grpBotonera = new qx.ui.groupbox.GroupBox();
			this._grpBotonera.setLayout(new qx.ui.layout.HBox(30).set({alignX:"center"}));
			this._grpBotonera.setWidth(670);
			this._btnAceptar = new qx.ui.form.Button("Aceptar");
			this._btnCancelar = new qx.ui.form.Button("Cancelar");
		this.debug("3-4");
		this._grpBotonera.add(this._btnAceptar);
		this._grpBotonera.add(this._btnCancelar);

	this.debug("4");
	
	this._grpStockManejo1.add(this._txtCantidad,{top:0,left:0});
	this._grpStockManejo1.add(this._txdFechaExtrae,{top:0,left:350});
	this._grpStockManejo1.add(this._txaMotivo,{top:30,left:0});
	this._grpStockManejo1.add(this._grpBotonera,{top:100,left:0});

	this.debug("5");
	this.base(arguments, "StockManejo", "Stock: Manejo");
	
	this.add(this._grpStockManejo, {top:0,left:0});
	this.add(this._grpStockManejo1, {top:330,left:140});
	this._attachEvents();
},

members: {
	
	_attachEvents : function (){
		this.addListener('appear', this._initForm, this);
		this._btnRefrescar.addListener('execute', this._tblStockRefresh, this);
		this._btnAceptar.addListener('execute', this._btnAceptarExecute, this);
	},
	_initForm : function(){
		try{
			var resultRubros = this._rpcSManejo.callSync("getRubros");
		}
		catch(ex){
			alert("Sync exception: " + ex);
		}
		this._cmbRubro.setNewValues(resultRubros, true);
		this._tblStockRefresh();
		this._txtCantidad.setValue("");
		this._txaMotivo.setValue("");
	},
	_tblStockRefresh : function(){
		try{
			var result = this._rpcSManejo.callSync("getStock", this._cmbRubro.getValue(), this._txtFiltro.getValue());
			this._mdlStockManejo.setData(result.values);
		}
		catch(ex){
			alert("Sync exception: " + ex);
		}
	},
	_btnAceptarExecute : function(){
		try{		
			var params = new Object;
			params.cod_articulo = this._mdlStockManejo.getValue(0, this._tblStockManejo.getFocusedRow());
			params.cantidad = this._txtCantidad.getValue();
			params.motivo = this._txaMotivo.getValue();
			params.fecha_egreso = this._txdFechaExtrae.getValueToSQL();
			this._rpcSManejo.callSync("insertExtraccion", params );
			this._initForm();
			
		}
		catch(ex){
			alert("Sync exception: " + ex);
		}			
	}
}
});
